package com.ths.inputservice.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ths.inputservice.R;

import java.util.List;

public class RtspHistoryAdapter extends ArrayAdapter<String> {
    
    private final LayoutInflater inflater;
    private final List<String> historyItems;
    
    public RtspHistoryAdapter(@NonNull Context context, @NonNull List<String> historyItems) {
        super(context, R.layout.item_rtsp_history, historyItems);
        this.inflater = LayoutInflater.from(context);
        this.historyItems = historyItems;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.item_rtsp_history, parent, false);
        }
        
        TextView textView = view.findViewById(R.id.tv_history_item);
        
        if (position < historyItems.size()) {
            textView.setText(historyItems.get(position));
        } else {
            // 最后一项是"关闭"
            textView.setText("关闭");
        }
        
        return view;
    }

    @Override
    public int getCount() {
        // 额外添加一个"关闭"选项
        return historyItems.size() + 1;
    }
}
